package com.yanqu.road.dao.impl.activity.sonhai;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiActivityData;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiPatronsData;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiUserData;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiVipAvePowerData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

public class SonHaiActivityDataDaoImpl extends TempDataObjectDao<SonHaiActivityData> {

    public static String getSonHaiActivityDataSql = "select * from `t_c_activity_son_hai_activity_data` where `activityId`=? and groupId = ?";
    public SonHaiActivityData getSonHaiActivityData(int activityId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        SonHaiActivityData sonHaiActivityData = new SonHaiActivityData(activityId, groupId);
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getSonHaiActivityDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                sonHaiActivityData = parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return sonHaiActivityData;
    }

    public static String updateSonHaiActivityDataSql = "update `t_c_activity_son_hai_activity_data` set maxRegionUniqueId=?,maxEggRewardId=?,eggResetTime=?,serverAvePowerMap=?,vipAvePowerMap=?,updateTeamScoreTime=?,bfEggRewardTime=? where activityId=? and groupId = ?";
    @Override
    protected boolean updateData(SonHaiActivityData sonHaiActivityData) {
        sonHaiActivityData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiActivityData.getMaxRegionUniqueId().get()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiActivityData.getMaxEggRewardId().get()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiActivityData.getEggResetTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, ""));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(sonHaiActivityData.getVipAvePowerMap())));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiActivityData.getUpdateTeamScoreTime()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiActivityData.getBfEggRewardTime()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiActivityData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiActivityData.getGroupId()));
        if (getDb().executeNoneQuery(updateSonHaiActivityDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSonHaiActivityDataSql = "insert into `t_c_activity_son_hai_activity_data`(activityId,groupId,maxRegionUniqueId,maxEggRewardId,eggResetTime,serverAvePowerMap,vipAvePowerMap,updateTeamScoreTime,bfEggRewardTime) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(SonHaiActivityData sonHaiActivityData) {
        sonHaiActivityData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiActivityData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiActivityData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiActivityData.getMaxRegionUniqueId().get()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiActivityData.getMaxEggRewardId().get()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiActivityData.getEggResetTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, ""));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(sonHaiActivityData.getVipAvePowerMap())));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiActivityData.getUpdateTeamScoreTime()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiActivityData.getBfEggRewardTime()));
        if (getDb().executeNoneQuery(insertSonHaiActivityDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SonHaiActivityData parseData(ResultSet rs) throws SQLException {
        SonHaiActivityData sonHaiActivityData = new SonHaiActivityData();
        sonHaiActivityData.setActivityId(rs.getInt("activityId"));
        sonHaiActivityData.setGroupId(rs.getInt("groupId"));
        AtomicInteger maxRegionUniqueId =  new AtomicInteger(rs.getInt("maxRegionUniqueId"));
        sonHaiActivityData.setMaxRegionUniqueId(maxRegionUniqueId);
        AtomicInteger maxEggRewardId = new AtomicInteger(rs.getInt("maxEggRewardId"));
        sonHaiActivityData.setMaxEggRewardId(maxEggRewardId);
        sonHaiActivityData.setEggResetTime(rs.getLong("eggResetTime"));
//        if (!StringUtils.isNullOrEmpty(rs.getString("serverAvePowerMap"))) {
//            Map<Long, Map<Integer, SonHaiVipAvePowerData>> map = JSON.parseObject(rs.getString("serverAvePowerMap"), new TypeReference<Map<Long, Map<Integer, SonHaiVipAvePowerData>>>() {});
//            sonHaiActivityData.setServerAvePowerMap(map);
//        }
        if (!StringUtils.isNullOrEmpty(rs.getString("vipAvePowerMap"))) {
            Map<Integer, Long> map = JSON.parseObject(rs.getString("vipAvePowerMap"), new TypeReference<Map<Integer, Long>>() {});
            sonHaiActivityData.setVipAvePowerMap(map);
        }
        sonHaiActivityData.setUpdateTeamScoreTime(rs.getLong("updateTeamScoreTime"));
        sonHaiActivityData.setBfEggRewardTime(rs.getLong("bfEggRewardTime"));
        return sonHaiActivityData;
    }
}
